package sample.util;

import javafx.scene.control.Button;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.ContentDisplay;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import sample.SceneBuilder;

import java.io.File;
import java.net.MalformedURLException;

/**

 * @name      :ButtonGenerator

 * @Description: the factory class for button generation
 * all buttons which are shown on the main stage generated here
 * a pure tool class

*/
public class ButtonGenerator{
    final static String iconPath="src/icon/";
    final static String[] icons={
            "paint","web","chat","game","like","more","snake"
    };
    final static String suffix=".png";
    public static ButtonBase paint() throws MalformedURLException {
//        File file=new File(iconPath+icons[0]+suffix);
        Button paintButton=MainButton(0);
        paintButton.setContentDisplay(ContentDisplay.TOP);
        paintButton.setOnMouseClicked(e-> SceneBuilder.PaintingPlace());
        return paintButton;
    }
    public static ButtonBase tic() throws MalformedURLException {
        //todo
        Button bt=MainButton(5);
        bt.setText("Tic-Tac-Toe");
        bt.setOnAction(e-> SceneBuilder.TicPlace());
        return bt;
    }
    public static ButtonBase like() throws MalformedURLException {
        Button likeButton=MainButton(4);
        likeButton.setContentDisplay(ContentDisplay.TOP);
        likeButton.setOnMouseClicked(e->{
            //todo
            SceneBuilder.Like();
//            new Alert("like",8,SceneBuilder.NullScene()).show();
        });
        return likeButton;
    }
    public static ButtonBase web() throws MalformedURLException {
        Button webButton=MainButton(1);
        webButton.setContentDisplay(ContentDisplay.TOP);
        webButton.setOnMouseClicked(e->{
            SceneBuilder.Web();
        });
        return webButton;
    }
    public static ButtonBase game() throws MalformedURLException {
        Button gameButton=MainButton(3);
        // todo
        // generate a stage
        gameButton.setOnMouseClicked(e->{
            switch (e.getButton()){
                case PRIMARY,MIDDLE->{
                    try {
                        SceneBuilder.GamePlace();
                    } catch (MalformedURLException ignored) {
                        new Alert("some problem occur",5).show();
                    }
                }
                case SECONDARY -> {
                    Ctl.HelpContextMenu().show(gameButton,e.getScreenX(),e.getScreenY());
                }
            }
        });
        return gameButton;
    }

    public static ButtonBase snake() throws MalformedURLException {
        Button snakeButton=MainButton(6);
        snakeButton.setOnAction(e-> new Alert("snake",10,SceneBuilder.NullScene()).show());
        return snakeButton;
    }
    private static Image ImageGenerator(int index) throws MalformedURLException {
        File file=new File(iconPath+icons[index]+suffix);
        return new Image(file.toURI().toURL().toString());
    }
    public static Image ImageGenerator(String name) throws MalformedURLException {
        return new Image(new File(iconPath+name+suffix)
                        .toURI().toURL().toString());
    }
    private static Button MainButton(int index) throws MalformedURLException {
        Button bt=new Button(icons[index],new ImageView(ImageGenerator(index)));
        bt.setContentDisplay(ContentDisplay.TOP);
        return bt;
    }
    public static Button SubButton(String name) throws MalformedURLException {
        Button bt=new Button(name,new ImageView(ImageGenerator(name)));
        bt.setContentDisplay(ContentDisplay.TOP);
        return bt;
    }
//    public static ButtonBase OperationSplitMenuButton(String title){
//
//    }
}

